package com.mmlc.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Administrator on 2016/7/23.
 */
public class PriceSelector extends View {
    public PriceSelector(Context context) {
        super(context);
        initParams();
    }

    public PriceSelector(Context context, AttributeSet attrs) {
        super(context, attrs);
        initParams();
    }

    public PriceSelector(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParams();
    }

    int width, height;
    DisplayMetrics metrics;
    float sizeScale;

    Paint circlePaint = new Paint();
    Paint linePaint = new Paint();

    private void initParams() {
        metrics = getResources().getDisplayMetrics();
        sizeScale = (float) metrics.widthPixels / 1080;

        linePaint.setAntiAlias(true);
        linePaint.setDither(true);
        linePaint.setColor(Color.parseColor("#a0a0a0"));
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeJoin(Paint.Join.ROUND);
        linePaint.setStrokeCap(Paint.Cap.ROUND);
        linePaint.setStrokeWidth(6);// 设置画笔粗细

        circlePaint.setAntiAlias(true);
        circlePaint.setDither(true);
        circlePaint.setColor(Color.parseColor("#000000"));
        circlePaint.setStyle(Paint.Style.FILL);
        circlePaint.setStrokeJoin(Paint.Join.ROUND);
        circlePaint.setStrokeCap(Paint.Cap.ROUND);
        circlePaint.setStrokeWidth(1);// 设置画笔粗细
    }

    /**
     * 左上右下 20*sizeScale空隙
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, (int) (80 * sizeScale));

        measureParams();
    }

    float lineL, lineT, lineR, lineB;
    float lineWidth, lineHeight;
    float selectorWidth, selectorHeight;

    boolean isFirst = true;

    private void measureParams() {
        width = getMeasuredWidth();
        height = getMeasuredHeight();

        lineL = (int) (40 * sizeScale);
        lineT = (int) (height / 2 - 3 * sizeScale);
        lineR = width - lineL;
        lineB = (int) (height / 2 + 3 * sizeScale);
        lineWidth = (int) (width - 80 * sizeScale);
        lineHeight = (int) (6 * sizeScale);

        selectorWidth = (int) (40 * sizeScale);
        selectorHeight = (int) (40 * sizeScale);
        if (isFirst && width > 0) {
            centerLeft = 40 * sizeScale;
            centerRight = width - 40 * sizeScale;
            isFirst = false;
        }
        radius = 20 * sizeScale;

//        Log.e("size2", width + "/" + height + "/" + lineL + "/" + lineT + "/" + lineR + "/" + lineB + "/" + selectorWidth + "/" + selectorHeight + "/" +
//                lineWidth + "/" + lineHeight + "/" + startL + "/" + startR);

        invalidate();
    }

    float radius;
    float centerLeft, centerRight;
    boolean leftSelected = false, rightSelected = false;

    float startX;
    float minPercent = 0, maxPercent = 1;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                checkSelected(event);
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = event.getX() - startX;
                if (leftSelected) {
                    if (centerLeft + radius + dx <= centerRight - radius && centerLeft + dx >= lineL) {
                        centerLeft += dx;
                    } else if (centerLeft + radius + dx > centerRight - radius) {
                        centerLeft = centerRight - radius * 2;
                        leftSelected = false;
                    } else if (centerLeft + dx < lineL) {
                        centerLeft = lineL;
                        leftSelected = false;
                    }
                    if (onSelectListener != null) {
                        minPercent = (centerLeft - lineL) / (lineR - lineL);
                        onSelectListener.minimum(minPercent);
                    }
                    invalidate();
                } else if (rightSelected) {
                    if (centerRight + dx <= lineR && centerRight - radius + dx >= centerLeft + radius) {
                        centerRight += dx;
                    } else if (centerRight - radius + dx < centerLeft + radius) {
                        centerRight = centerLeft + radius * 2;
                        rightSelected = false;
                    } else if (centerRight + dx > lineR) {
                        centerRight = lineR;
                        rightSelected = false;
                    }
                    if (onSelectListener != null) {
                        maxPercent = (centerRight - lineL) / (lineR - lineL);
                        onSelectListener.maximum(maxPercent);
                    }
                    invalidate();
                }
                startX = event.getX();
                break;
            case MotionEvent.ACTION_UP:
                if (leftSelected || rightSelected) invalidate();
                leftSelected = false;
                rightSelected = false;
                break;
        }
        return true;
    }

    private void checkSelected(MotionEvent event) {
        if (event.getX() < centerLeft + radius && event.getX() > centerLeft - radius * 2) {
            leftSelected = true;
            rightSelected = false;
        } else if (event.getX() > centerRight - radius && event.getX() < centerRight + radius * 2) {
            leftSelected = false;
            rightSelected = true;
        } else {
            leftSelected = false;
            rightSelected = false;
        }
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        canvas.drawLine(lineL, lineT, lineR, lineB, linePaint);
        canvas.drawCircle(centerLeft, height / 2, radius, circlePaint);
        canvas.drawCircle(centerRight, height / 2, radius, circlePaint);
    }

    public float getMinPercent() {
        return minPercent;
    }

    public void setMinPercent(float minPercent) {
        if (minPercent < 0 || minPercent > 1 || minPercent >= maxPercent) return;
        this.minPercent = minPercent;
        centerLeft = lineL + (lineR - lineL) * minPercent;
        if (centerLeft > centerRight - radius * 2) {
            centerLeft = centerRight - radius * 2;
            minPercent = (centerLeft - lineL) / (lineR - lineL);
        }
        if (onSelectListener != null) onSelectListener.minimum(minPercent);
        invalidate();
    }

    public float getMaxPercent() {
        return maxPercent;
    }

    public void setMaxPercent(float maxPercent) {
        if (maxPercent < 0 || maxPercent > 1 || maxPercent <= minPercent) return;
        this.maxPercent = maxPercent;
        centerRight = lineL + (lineR - lineL) * maxPercent;
        if (centerRight < centerLeft + radius * 2) {
            centerRight = centerLeft + radius * 2;
            maxPercent = (centerRight - lineL) / (lineR - lineL);
        }
        if (onSelectListener != null) onSelectListener.maximum(maxPercent);
        invalidate();
    }

    OnSelectListener onSelectListener;

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    public interface OnSelectListener {
        void minimum(float min);

        void maximum(float max);
    }
}
